<HTML>
<HEAD>
<TITLE>[Chapter 6] 6.3 Insets</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:36:46 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch06_02.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 6<br>Containers</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch06_04.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-6-SECT-3">6.3 Insets</A></h2>

<P CLASS=para>
<A NAME="CH06.INSET"></A>The <tt CLASS=literal>Insets</tt> class provides a 
way to encapsulate the layout margins of the four different sides of a 
container. The class helps in laying out containers. The <tt CLASS=literal>Container</tt> 
can retrieve their values through the <tt CLASS=literal>getInsets()</tt> 
method, then analyze the settings to position components. The different 
inset values are measured in pixels. The space reserved by insets can still 
be used for drawing directly within <tt CLASS=literal>paint()</tt>. 
Also, if the <tt CLASS=literal>LayoutManager</tt> 
associated with the container does not look at the insets, the request 
will be completely ignored. 

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-6-SECT-3.1">Insets Methods</A></h3>Variables

<P CLASS=para>
There are four variables for insets, one for each border. 

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public int top </I><br>
<DD>

<P CLASS=para>
This variable contains the border width in pixels for the top of a container. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public int bottom </I><br>
<DD>

<P CLASS=para>
This variable contains the border width in pixels for the bottom of a container. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public int left </I><br>
<DD>

<P CLASS=para>
This variable contains the border width in pixels for the left edge of a container. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public int right </I><br>
<DD>

<P CLASS=para>
This variable contains the border width in pixels for the right edge of a container. </DL>
Constructors

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public Insets (int top, int left, int bottom, int right) </I><br>
<DD>

<P CLASS=para>
The constructor creates an <tt CLASS=literal>Insets</tt> 
object with <tt CLASS=literal>top</tt>, <tt CLASS=literal>left</tt>, 
<tt CLASS=literal>bottom</tt>, and <tt CLASS=literal>right</tt> 
being the size of the insets in pixels. If this object was the return object 
from the <tt CLASS=literal>getInsets()</tt> method 
of a container, these values represent the size of a border inside that 
container. </DL>
Miscellaneous methods

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public Object clone ()  </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>clone()</tt> method creates 
a clone of the <tt CLASS=literal>Insets</tt> so the 
same <tt CLASS=literal>Insets</tt> object can be associated with multiple containers. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public boolean equals(Object object) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>equals()</tt> method defines 
equality for insets. Two <tt CLASS=literal>Insets</tt> 
objects are equal if the four settings for the different values are equal. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public String toString () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>toString()</tt> method of <tt CLASS=literal>Insets</tt> 
returns the current settings. Using the <tt CLASS=literal>new Insets (10, 
20, 30, 40)</tt> constructor, the results would be: </DL>
<DIV CLASS=screen>
<P>
<PRE>
java.awt.Insets[top=10,left=20,bottom=30,right=40]
</PRE>
</DIV>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-6-SECT-3.2">Insets Example</A></h3>

<P CLASS=para>
The following source code demonstrates the use of insets within an applet's 
<tt CLASS=literal>Panel</tt>. The applet displays 
a button that takes up the entire area of the <tt CLASS=literal>Panel</tt>, 
less the insets, then draws a rectangle around that area. This 
is shown visually in <A HREF="ch06_03.htm#JAWT-CH-6-FIG-1">Figure 6.1</A>. The example demonstrates 
that if you add components to a container, the <tt CLASS=literal>LayoutManager</tt> 
deals with the insets for you in positioning them. But if you are drawing 
directly to the <tt CLASS=literal>Panel</tt>, you 
must look at the insets if you want to avoid the requested area within 
the container. 

<DIV CLASS=screen>
<P>
<PRE>
import java.awt.*;
import java.applet.*;
public class myInsets extends Applet {
    public Insets insets () {
        return new Insets (50, 50, 50, 50);
    }
    public void init () {
        setLayout (new BorderLayout ());
        add ("Center", new Button ("Insets"));
    }
    public void paint (Graphics g) {
        Insets i = insets();
        int width  = size().width - i.left - i.right;
        int height = size().height - i.top - i.bottom;
        g.drawRect (i.left-2, i.top-2, width+4, height+4);
        g.drawString ("Insets Example", 25, size().height - 25);
    }
}
</PRE>
</DIV>

<DIV CLASS=figure>
<h4 CLASS=figure><A CLASS="TITLE" NAME="JAWT-CH-6-FIG-1">Figure 6.1: Insets</A></h4>


<p>
<img align=middle src="./figs/jawt0601.gif" alt="[Graphic: Figure 6-1]" width=193 height=248 border=0>

</DIV>

<P CLASS=para>
To change the applet's insets from the default, we override the <tt CLASS=literal>insets()</tt> method to return a new <tt CLASS=literal>Insets</tt> object, with the new values.

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch06_02.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch06_04.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Panel</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Window</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
